#ifndef __UTIL_H__
#define __UTIL_H__
#include <string>
#include <memory>
#include <vector>
#include <jsoncpp/json/json.h>
#include <mysql/mysql.h>
#include <fstream>
#include "logger.hpp"
using namespace std;
class mysql_util
{
public:
    // 初始化
    static MYSQL *mysql_create(string host,
                               string username,
                               string password,
                               string dbname,
                               int port = 7306)
    {
        MYSQL *mysql = mysql_init(NULL);
        mysql = mysql_real_connect(mysql, host.c_str(), username.c_str(), password.c_str(), dbname.c_str(), port, NULL, 0);
        if (mysql == NULL)
        {
            ELOG("mysql_real_connect fail");
            return NULL;
        }
        int n = mysql_set_character_set(mysql, "utf8");
        if (n != 0)
        {
            ELOG("mysql_set_character_set fail");
            return NULL;
        }
        return mysql;
    }
    // 执行sql语句
    static bool mysql_exec(MYSQL *mysql, const string &sql)
    {
        int n = mysql_query(mysql, sql.c_str());
        if (n != 0)
        {
            ELOG("%s", sql.c_str());
            ELOG("mysql_query fail");
            return false;
        }
        return true;
    }
    // 释放内存
    static void mysql_destroy(MYSQL *mysql)
    {
        mysql_close(mysql);
    }
};
class Json_util
{
public:
    static bool serialize(const Json::Value &root, string &str)
    {
        // 实例化出StreamWriterBuilder工厂类对象
        Json::StreamWriterBuilder swb;
        // 生成StreanWwriter对象
        unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        // 对Json::Value对象反序列化
        std::stringstream ret;
        int n = sw->write(root, &ret);
        str = ret.str();
        if (n != 0)
        {
            ELOG("serialize fail");
            return false;
        }
        return true;
    }
    static bool unserialize(const string &str, Json::Value &root)
    {
        // 创建CharReaderBuilder工程类对象
        Json::CharReaderBuilder crb;
        // 生成CharReader对象
        unique_ptr<Json::CharReader> cr(crb.newCharReader());
        // 把字符数据转化成Json::Value
        bool f = cr->parse(str.c_str(), str.c_str() + str.size(), &root, nullptr);
        if (!f)
        {
            ELOG("parse fail");
            return false;
        }
        return true;
    }
};
class String_util
{
public:
    static int splits(const string &src, const string sep, vector<string> &res)
    {
        size_t pos, idx = 0;
        while (idx < src.size())
        {
            pos = src.find(sep, idx);
            if (pos == string::npos)
            {
                res.push_back(src.substr(idx));
                break;
            }
            if (pos == idx)
            {
                idx = pos + sep.size();
                continue;
            }
            res.push_back(src.substr(idx, pos - idx));
            idx = pos + sep.size();
        }
        return res.size();
    }
};
class file_util
{
public:
    static bool read(const string& filename,string& body)
    {
        //打开文件
        ifstream ifs(filename.c_str(),std::ios::binary);
        if(ifs.is_open()==false)
        {
            ELOG("%s open fail",filename.c_str());
            return false;
        }
        //获取文件大小
        size_t fsz=0;
        ifs.seekg(0,std::ios::end);
        fsz=ifs.tellg();
        ifs.seekg(0,std::ios::beg);
        body.resize(fsz);
        //读取文件
        ifs.read(&body[0],fsz);
        if(ifs.good()==false)
        {
            ELOG("%s read fail",filename.c_str());
            return false;
        }
        //关闭文件
        ifs.close();
        return true;
    }
};
#endif